package com.gusti.turnos.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.gusti.turnos.dao.IAppointmentDao;
import com.gusti.turnos.exception.TurnosException;
import com.gusti.turnos.model.Appointment;
import com.gusti.turnos.util.Debug;

/**
 * 
 * @author gusti
 *
 */
@Repository
public class AppointmentDao extends ProjectDao<Appointment> implements IAppointmentDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Appointment> retrieveAppointments() {

		List<Appointment> appointments = new ArrayList<Appointment>();
		Query query = null;
		try {
			query = em.createQuery( "FROM Appointment as ap ORDER BY ap.name" );
			appointments = (List<Appointment>)query.getResultList();
		} catch ( Exception e ) {
			Debug.error( e );
			throw new TurnosException( e );
		}

		return appointments;
	}

	@Override
	public Appointment retrieveAppointment( long id ) {

		Appointment appointment = new Appointment();
		Query query = null;
		try {
			query = em.createQuery( "FROM Appointment WHERE id = :id" );
			query.setParameter( "id", id );
			appointment = (Appointment)query.getSingleResult();
		} catch ( Exception e ) {
			Debug.error( e );
			throw new TurnosException( e );
		}

		return appointment;
	}
}
